package cn.picclife.mwx.manager.sys.service.impl;

import java.security.SecureRandom;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.FlowPathMapper;
import cn.picclife.mwx.manager.sys.mapper.PanelPointMapper;
import cn.picclife.mwx.manager.sys.model.po.FlowPath;
import cn.picclife.mwx.manager.sys.model.po.PanelPoint;
import cn.picclife.mwx.manager.sys.service.PanelPointService;

/**
 * @ClassName PanelPointServiceImpl.java
 * @Description 流程节点信息业务层实现类
 * @author Guan JunHui
 * @Date 2017年9月14日
 */
@Service
public class PanelPointServiceImpl implements PanelPointService {
	
	@Autowired
	private PanelPointMapper panelPointMapper;
	
	@Autowired
	private FlowPathMapper flowPathMapper;
	

	/**
	 * 插入流程节点信息
	 */
	@Override
	public int insertPanelPoint(PanelPoint panelPoint) throws Exception{
		//将流程基本信息的节点个数加1
		Map<String, Object>map = flowPathMapper.searchByFlowPathId(panelPoint.getFlowpath_Id());
		FlowPath flowPath = new FlowPath();
		flowPath.setNum((int)map.get("num")+1);
		flowPathMapper.updateFlowPath(flowPath);
		
		StringBuilder str=new StringBuilder();//定义变长字符串
		//Random random=new Random();
		SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
		//随机生成数字，并添加到字符串
		for(int i=0;i<8;i++){
		    str.append(random.nextInt(10));
		}
		//将字符串转换为数字并输出
		int num=Integer.parseInt(str.toString());
		panelPoint.setPanelpoint_id(num);
		panelPoint.setCreateTime(new Date());
		//在插入前,将顺序大于等于该新纪录顺序的注意加一  若新纪录order字段位空,则默认为最后一个
		if (panelPoint.getSequence() == 0) {
			panelPoint.setSequence(panelPointMapper.countPaingPointByFlowPatnId(panelPoint.getFlowpath_Id())+1);
		}else{
			if (panelPoint.getSequence() < panelPointMapper.countPaingPointByFlowPatnId(panelPoint.getFlowpath_Id())) {
				panelPointMapper.updateSequence(panelPoint);
			}
			panelPoint.setSequence(panelPoint.getSequence()+1);
		}
		//插入数据
		num = panelPointMapper.insertPanelPoint(panelPoint);
		return num;
	}

	/**
	 * 根据流程基本信息主键查询对应的流程节点信息
	 */
	@Override
	public List<PanelPoint> queryPanelPoints(int flowPath_id) {
		return panelPointMapper.queryPanelPoint(flowPath_id);
	}

	/**
	 * 根据主键进行删除
	 */
	@Override
	public int deletePanelPoint(PanelPoint panelPoint) {
		//删除之后将被删除的数据后面的记录的顺序减一
		panelPoint = panelPointMapper.queryPanelPointByPanelId(panelPoint.getPanelpoint_id());
		panelPointMapper.updateSequenceForDel(panelPoint);
		int flag = 0;
		try {
			panelPointMapper.deletePanelPoint(panelPoint.getPanelpoint_id());
			Map<String, Object>map = flowPathMapper.searchByFlowPathId(panelPoint.getFlowpath_Id());
			FlowPath flowPath = new FlowPath();
			flowPath.setNum((int)map.get("num")+1);
			flowPathMapper.updateFlowPath(flowPath);
			flag = 1;
		} catch (Exception e) {
			flag = 0;
		}
		return flag;
	}
	
	/**
	 * 修改流程节点信息
	 */
	@Override
	public int updatepanelPoint(PanelPoint panelPoint){
		// 1  2  3  4  5   12453
		
		PanelPoint oldPanelPoint = panelPointMapper.queryPanelPointByPanelId(panelPoint.getPanelpoint_id());
		panelPoint.setEditTime(new Date());
		panelPoint.setFlowpath_Id(oldPanelPoint.getFlowpath_Id());
		//如果顺序为0,那么就放到最后
		if (panelPoint.getSequence() == 0) {
			int newSequence = panelPointMapper.countPaingPointByFlowPatnId(panelPoint.getFlowpath_Id());
			List<PanelPoint> list = panelPointMapper.queryPanelPointBySequence(panelPoint.getFlowpath_Id(), oldPanelPoint.getSequence(), newSequence);
			for (PanelPoint panelPoint2 : list) {
				panelPoint2.setSequence(panelPoint2.getSequence()-1);
				panelPointMapper.updatePanelPoint(panelPoint2);
			}
			panelPoint.setSequence(newSequence);
			return panelPointMapper.updatePanelPoint(panelPoint);
		}
		
		//顺序未修改  直接更改
		if (oldPanelPoint.getSequence()-1==panelPoint.getSequence()) {
			return panelPointMapper.updatePanelPoint(panelPoint);
		}
		//现在的顺序大于原来的顺序
		if (panelPoint.getSequence()>oldPanelPoint.getSequence()) {
			List<PanelPoint>list = panelPointMapper.queryPanelPointBySequence(panelPoint.getFlowpath_Id(), oldPanelPoint.getSequence(), panelPoint.getSequence());
			for (PanelPoint panelPoint2 : list) {
				panelPoint2.setSequence(panelPoint2.getSequence()-1);
				panelPointMapper.updatePanelPoint(panelPoint2);
			}
			return panelPointMapper.updatePanelPoint(panelPoint);
		}else {
			List<PanelPoint>list = panelPointMapper.queryPanelPointBySequence(panelPoint.getFlowpath_Id(), panelPoint.getSequence(), oldPanelPoint.getSequence());
			for (PanelPoint panelPoint2 : list) {
				panelPoint2.setSequence(panelPoint2.getSequence()+1);
				panelPointMapper.updatePanelPoint(panelPoint2);
			}
			panelPoint.setSequence(panelPoint.getSequence()+1);
			return panelPointMapper.updatePanelPoint(panelPoint);
		}
	}

	@Override
	public PanelPoint queryPanelPointByPanelId(int panelpoint_id) {
		return panelPointMapper.queryPanelPointByPanelId(panelpoint_id);
	}

}
